package sql

import org.apache.spark.SparkConf
import org.apache.spark.sql.expressions.Aggregator
import org.apache.spark.sql._

object SparkSql4 {
  //自定义聚合函数类，需要继承Aggregator
  def main(args:Array[String]):Unit= {
    val sparkconf = new SparkConf().setMaster("local[*]").setAppName("sparkSql")
    val spark = SparkSession.builder().config(sparkconf).getOrCreate()
    //读取mysql数据库
    val df: DataFrame = spark.read
      .format("jdbc")
      .option("url", "172.16.136.26:1527")
      .option("driver", "jdbc:snappydata://172.16.136.26:1527/")
      .option("user", "app")
      .option("password", "app")
      .option("dbtable", "application_bigdata_atgd")
      .load()
    df.show(20)
    //保存数据
//    df.write
//      .format("jdbc")
//      .option("url", "jdbc:mysql://localhost:3306/ruo_vue?useUnicode=true&characterEncoding=utf-8&useSSL=false")
//      .option("driver", "com.mysql.jdbc.Driver")
//      .option("user", "root")
//      .option("password", "123456789")
//      .option("dbtable", "sys_menu1")
//      .mode(SaveMode.Append)
//      .save()

    spark.close()

  }

}

